<template>
    <div style="height:100%;width:100%;position:relative;">
        <div class="pagetop">
            <div class="search-title">
                <table>
                    <tr>
                        <!-- 查询条件 -->
                        <td>
                            快速查询
                        </td>
                        <td>
                            <el-input size="small" style="width: 200px" v-model="searchStr"
                                @keyup.enter.native="InitPageData" placeholder="输入医院代码|医院名称|所属区域"></el-input>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="pagecenter">
            <div class="middle-center" style="height:100%;">
                <vxe-table :data="hospitalData" border height="100%" ref="hospital_table" highlight-current-row
                    @cell-click="hospitalClick" highlight-hover-column highlight-hover-row how-header-overflow resizable
                    show-overflow size="mini" stripe show-header-overflow>
                    <vxe-table-column field="hospcode" sortable title="医院代码" width="160" header-align="center"
                        align="center" show-overflow>
                    </vxe-table-column>
                    <vxe-table-column field="hospitalname" sortable title="医院名称" width="200" header-align="center"
                        align="left" show-overflow>
                    </vxe-table-column>
                    <vxe-table-column field="hospna" sortable title="医院简称" width="110" header-align="center"
                        align="left" show-overflow>
                    </vxe-table-column>
                    <vxe-table-column field="regionname" sortable title="所属区域" width="100" header-align="center"
                        align="center" show-overflow>
                    </vxe-table-column>
                    <vxe-table-column field="barheader" sortable title="条码前缀" width="70" header-align="center"
                        align="center" show-overflow>
                    </vxe-table-column>
                    <vxe-table-column field="barseqlen" sortable title="流水号长度" width="70" header-align="center"
                        align="center" show-overflow>
                    </vxe-table-column>
                </vxe-table>
            </div>
            <div class="middle-right">
                <div style="height:26%">
                    <table class="edit" style="padding:5px;border-collapse:separate; border-spacing:3px 4px;">
                        <tr>
                            <td>最后打印流水号</td>
                            <td>
                                <el-input style="width:100%;" v-model="barcodeinfo.lastend_num" :disabled="true">
                                </el-input>
                            </td>
                        </tr>
                        <tr>
                            <td>起始流水号</td>
                            <td>
                                <el-input style="width:100%;" v-model="barcodeinfo.start_num" :disabled="true">
                                </el-input>
                            </td>
                        </tr>
                        <tr>
                            <td>延续个数</td>
                            <td>
                                <el-input style="width:100%;" v-on:input="changeBarcode" v-model="barcodeinfo.quantity">
                                </el-input>
                            </td>
                            <td></td>
                            <td>
                            </td>
                        </tr>
                        <tr>
                            <td>号段</td>
                            <td>
                                <el-input style="width:100%;" v-model="barcodeinfo.start_barcode" :disabled="true">
                                </el-input>
                            </td>
                            <td> - </td>
                            <td>
                                <el-input style="width:100%;" v-model="barcodeinfo.end_barcode" :disabled="true">
                                </el-input>
                            </td>
                        </tr>
                    </table>
                    <table>
                        <tr>
                            <td>
                                <el-button @click="btnPrint" type="primary" icon="el-icon-printer">打印标签</el-button>
                            </td>
                            <td>
                                <el-button v-if="rptStyleSettingState" @click="rptStyleSetting" class="el-icon-printer">
                                    打印样式设计</el-button>
                            </td>
                            <td>
                                <rm-print-comm prnt_type="barcode"></rm-print-comm>
                            </td>
                        </tr>
                    </table>
                </div>
                <div style="height:74%">
                    <vxe-table :data="barcodePrintDate" border height="100%" ref="barcodeprint_table"
                        highlight-current-row highlight-hover-column highlight-hover-row how-header-overflow resizable
                        show-overflow size="mini" stripe show-header-overflow>
                        <vxe-table-column field="start_barcode" sortable title="起始条码号" width="180" header-align="center"
                            align="center" show-overflow>
                        </vxe-table-column>
                        <vxe-table-column field="end_barcode" sortable title="截止条码号" width="180" header-align="center"
                            align="center" show-overflow>
                        </vxe-table-column>
                        <vxe-table-column field="quantity" sortable title="延续个数" width="100" header-align="center"
                            align="center" show-overflow>
                        </vxe-table-column>
                        <vxe-table-column field="print_username" sortable title="打印人" width="120" header-align="center"
                            align="center" show-overflow>
                        </vxe-table-column>
                        <vxe-table-column field="print_time" sortable title="打印时间" width="150" header-align="center"
                            align="center" show-overflow>
                        </vxe-table-column>
                    </vxe-table>
                </div>
            </div>
        </div>
    </div>
</template>

<script type="text/javascript">
export default {
    name: "LIS260",
    data() {
        return {
            searchStr: "",
            hospitalData: [],
            barcodePrintDate: [],
            barcodeinfo: {
                select_hospcpde: "",
                select_hospname: "",
                select_barheader: "",
                select_barseqlen: "",
                lastend_num: "",
                start_num: "",
                quantity: "",
                start_barcode: "",
                end_barcode: "",
                end_num: "",
            },
            Str: [],
            rptStyleSettingState: false,
        };
    },
    created() {
        this.InitPageData();
        this.getLoginutype();
    },
    mounted() { },
    updated() { },
    methods: {
        InitPageData() {
            this.$tool
                .request({
                    url: "LIS/LIS2/LIS260",
                    method: "post",
                    data: {
                        action: "InitPageData",
                        searchStr: this.searchStr,
                    },
                })
                .then((res) => {
                    this.hospitalData = res.Data;
                });
        },
        getLoginutype() {
            let loginutype = this.$tool.getSession(this.$tool.loginutype);
            if (loginutype == "G") {
                this.rptStyleSettingState = true;
            }
        },
        // 打印样式设计
        rptStyleSetting() {
            let strJson = JSON.stringify([{ title: "条码号", property: "barcode"}, { title: "医院名称", property: "hospname" },{title: "医院简称", property: "hospna"}]);
            strJson += "\r";
            strJson += JSON.stringify([{ barcode: 'abc0000333', hospname: '测试医院名称' ,hospna: "测试医院"}, { barcode: 'abc0000334', hospname: '测试医院名称' ,hospna: "测试医院"}, { barcode: 'abc0000335', hospname: '测试医院名称' ,hospna: "测试医院"}]);//预览数据
            let sql = btoa(encodeURIComponent(strJson));
            jsObj.SetCustomJsonPrintConfig("5095", sql, this.$tool.getSession(this.$tool.loginuserno), "预制条码打印样式");
        },
        hospitalClick({ row, rowIndex }) {
            this.barcodeinfo.select_hospcpde = row.hospcode;
            this.barcodeinfo.select_hospname = row.hospitalname;
            this.barcodeinfo.select_hospna = row.hospna;
            this.barcodeinfo.select_barheader = row.barheader;
            this.barcodeinfo.select_barseqlen = row.barseqlen;
            if (!row.barheader) {
                this.$message.error("选择的医院，条码前缀未维护");
                this.barcodeinfo.lastend_num = "";
                this.barcodeinfo.start_num = "";
                this.barcodePrintDate=[];
                return false;
            }
            if (!row.barseqlen) {
                this.$message.error("选择的医院，流水号长度未维护");
                this.barcodeinfo.lastend_num = "";
                this.barcodeinfo.start_num = "";
                return false;
            }
            this.getBarcodeHistory(row.hospcode);
        },
        getBarcodeHistory(hospcode) {
            this.$tool
                .request({
                    url: "LIS/LIS2/LIS260",
                    method: "post",
                    data: {
                        action: "barcodeprinthistory",
                        hospcode: hospcode,
                    },
                })
                .then((res) => {
                    this.barcodePrintDate = res.Data;
                    this.clearBarcodeInfo();
                    if (res.Data.length > 0) {
                        this.barcodeinfo.lastend_num = parseInt(res.Data[0].end_num);
                        this.barcodeinfo.start_num = parseInt(res.Data[0].end_num) + 1;
                    }
                    else {
                        this.barcodeinfo.lastend_num = 0;
                        this.barcodeinfo.start_num = 1;
                    }
                });
        },
        changeBarcode() {
            this.barcodeinfo.quantity = this.barcodeinfo.quantity.replace(/[^0-9]/g, '');

            if (this.barcodeinfo.quantity.length > 0 && this.barcodeinfo.quantity != 0 && this.barcodeinfo.start_num) {

                var h_length = this.barcodeinfo.select_barheader.length;//前缀长度
                var b_length = parseInt(this.barcodeinfo.select_barseqlen);//流水号长度

                //检验初始号段
                var s_length = (this.barcodeinfo.start_num + "").length;//起始流水号长度
                if (parseInt(b_length) - parseInt(s_length) - parseInt(h_length) < 0) {
                    this.$message.error("输入的延续个数有误，起始条码号超出了流水号长度");
                    this.barcodeinfo.quantity = "";
                    return false;
                }
                //检验截止号段
                var end_num = parseInt(this.barcodeinfo.start_num) + parseInt(this.barcodeinfo.quantity) - 1;
                this.barcodeinfo.end_num = end_num;
                var e_length = (end_num + "").length;//截止流水号长度
                if (parseInt(b_length) - parseInt(e_length) - parseInt(h_length) < 0) {
                    this.$message.error("输入的延续个数有误，截止条码号超出了流水号长度");
                    return false;
                }

                var start_barcode = this.barcodeinfo.select_barheader;
                var end_barcode = this.barcodeinfo.select_barheader;
                //计算初始条码号
                for (let index = 0; index < parseInt(b_length) - parseInt(s_length) - parseInt(h_length); index++) {
                    start_barcode += "0";
                }
                start_barcode += this.barcodeinfo.start_num + "";

                //计算截止条码号
                for (let index = 0; index < parseInt(b_length) - parseInt(e_length) - parseInt(h_length); index++) {
                    end_barcode += "0";
                }
                end_barcode += end_num + "";

                //填充打印数组
                this.Str = [];
                for (let index = 0; index < parseInt(this.barcodeinfo.quantity); index++) {
                    //拼接条码号
                    var barcode = this.barcodeinfo.select_barheader;
                    var num = parseInt(this.barcodeinfo.start_num) + index;
                    var n_length = (num + "").length;
                    for (let index2 = 0; index2 < parseInt(b_length) - parseInt(h_length) - parseInt(n_length); index2++) {
                        barcode += "0";
                    }
                    barcode += num + "";
                    this.Str.push({ barcode: barcode, hospname: this.barcodeinfo.select_hospname, hospna: this.barcodeinfo.select_hospna });
                }

                this.barcodeinfo.start_barcode = start_barcode;
                this.barcodeinfo.end_barcode = end_barcode;
            } else {
                this.barcodeinfo.quantity = "";
                this.barcodeinfo.start_barcode = "";
                this.barcodeinfo.end_barcode = "";
                if (!this.barcodeinfo.start_num) {
                    this.$message.error("请先选择医院或者检查所选医院的条码前缀与流水号长度是否维护");
                    return false;
                }
            }
        },
        btnPrint() {
            if (this.barcodeinfo.quantity) {
                this.$tool
                    .request({
                        url: "LIS/LIS2/LIS260",
                        method: "post",
                        data: {
                            action: "savePrintData",
                            hospcode: this.barcodeinfo.select_hospcpde,
                            hospname: this.barcodeinfo.select_hospname,
                            start_barcode: this.barcodeinfo.start_barcode,
                            end_barcode: this.barcodeinfo.end_barcode,
                            end_num: this.barcodeinfo.end_num,
                            quantity: this.barcodeinfo.quantity,
                        },
                    })
                    .then((res) => {
                        if (res.flag == 1) {
                            this.getBarcodeHistory(this.barcodeinfo.select_hospcpde);
                            //打印
                            for (let index = 0; index < this.Str.length; index++) {
                                const element = this.Str[index];
                                var arr = [];
                                arr.push(element);
                                //title
                                let strJson = JSON.stringify([{ title: "条码号", property: "barcode"}, { title: "医院名称", property: "hospname" },{title: "医院简称", property: "hospna"}]);
                                strJson += "\r";
                                strJson += JSON.stringify(arr);//数据
                                let sql = btoa(encodeURIComponent(strJson));
                                jsObj.PrintCustomJsonReport("5095", sql, this.$tool.getSession(this.$tool.loginuserno), "预制条码打印", "");
                            }
                            this.Str = [];//打印完成清除数据，避免重复打印
                        }
                    });
            }
        },
        clearBarcodeInfo() {
            this.barcodeinfo.lastend_num = "";
            this.barcodeinfo.start_num = "";
            this.barcodeinfo.quantity = "";
            this.barcodeinfo.start_barcode = "";
            this.barcodeinfo.end_barcode = "";
            this.barcodeinfo.end_num = "";
        },
        PrintSetingSave(prntdata, s) {
            s.optionvalue = prntdata.prnt_name;
        },
    },
};
</script>

<style scoped>
.pagetop {
    left: 0;
    right: 0;
    height: 35px;
    -webkit-box-pack: left;
    -webkit-box-align: center;
    border: solid 1px #ccc;
    position: absolute;
    z-index: 99;
}

.pagecenter {
    position: absolute;
    top: 40px;
    width: 100%;
    left: 0;
    bottom: 0px;
    overflow: auto;
}

.middle-center {
    position: absolute;
    height: 100%;
    right: 50%;
    left: 0;
    overflow: auto;
}

.middle-right {
    position: absolute;
    top: 0;
    bottom: 0px;
    overflow: visible;
    width: 50%;
    right: 0px;
}

.search-title {
    margin: 0 5px 0px 4px;
    top: 0px;
    font-size: 12px;
}

table {
    font-size: 12px;
    border-collapse: separate;
    border-spacing: 5px;
}
</style>
